/*构建查询区
 * ---1：storesql不是sql的处理 getOptions
 * ---2：label类型增加参数控制是单选还是多选
 * ---3：支持默认值 页面参数传入 如数据库 主题
 * -4：更多选项 收缩
 * --5：新增对树的支持
 */
var getItemData = function(sql,val){
	var data = [];
	if (sql.toLowerCase().indexOf("select ") >= 0) {
		data=ai.getStoreData(sql);
	} else if (sql.indexOf("|") >= 1) {
		var tmpArray = item.storesql.split("|");
		for (var i = 0, tmpLen = tmpArray.length; i < tmpLen; i++) {
			data.push({
				value: tmpArray[i].split(",")[0],
				name: tmpArray[i].split(",")[1]
			});
		}
	} else {
		data = sql.split(",");
	}
	return data;
}
var rebuildSQL = function(sql,val){
	var rs="";
	if(this.DEFAULT_ALL_SELECT||val){
		rs = sql.replace(/{val}/g,val);
	}else{
		var sqlArr = sql.split(/\swhere\s|\sand\s/i);
		for(var i=0;i<sqlArr.length;i++){
			var _sqlSplit = sqlArr[i];
			if(/{val}/g.test(_sqlSplit)){
				sqlArr[i] = " 1=1 "
			}
			rs += (sqlArr[i]+(i==0&&sqlArr.length!=1?" where ":i==sqlArr.length-1?"":" and "));
		}
	}
	return rs;
};
var metadbListPermission = function(){
	var ret="";
	if(_UserInfo.username=='sys'){
		ret="*";
		return ret;
	}
	var sql = "SELECT DISTINCT dbname FROM meta_team_role_user a,metadb_list_permission b WHERE username = '"+_UserInfo.username+"' AND a.TEAM_CODE = b.team_code";
	var metadbListPermissionStore = ai.getJsonStore(sql);
	for(i=0;i<metadbListPermissionStore.root.length;i++){
		ret+="'"+metadbListPermissionStore.root[i].DBNAME+"',";
	}
	return ret.substring(0,ret.length-1);
}
var rebuildSqlByPermission = function(sql,permissionStr,labalName){
	if(paramMap['prod'] && paramMap['prod']=='dataos' && permissionStr !='' && permissionStr !='*' && (labalName=="数据库" || labalName=="业务主题" || labalName=="数据权限")){
		if(sql.toLowerCase().indexOf(' where ')>0){
			sql += " and dbname in ("+permissionStr+")";					
		}else{
			sql += " where dbname in ("+permissionStr+")";
		}
	}
	return sql;
}
var showOrHidden=function(e){
	var filter=$(e).parent();
	if(filter.find("article[name='hiddenArt']:hidden").length>0){
		filter.find("article[name='hiddenArt']").css("display","block");
		filter.find("div[class='item-more']").html('<p style="width:45%;float:left;height:23px;border-top:1px solid #DDD"></p><p style="width:10%;height:23px;float:left;border-bottom:1px solid #DDD;border-left:1px solid #DDD;border-right:1px solid #DDD;text-align:center;font-size:15px;line-height:23px;color:#DDD">收起&nbsp;<span style="font-size:18px;color:#DDD">&and;</span></p><p style="width:45%;float:left;height:23px;border-top:1px solid #DDD"></p>');			
	}else{
		filter.find("article[name='hiddenArt']").css("display","none");
		filter.find("div[class='item-more']").html('<p style="width:43%;float:left;height:23px;border-top:1px solid #DDD"></p><p style="width:14%;height:23px;float:left;border-bottom:1px solid #DDD;border-left:1px solid #DDD;border-right:1px solid #DDD;text-align:center;font-size:15px;line-height:23px;color:#DDD">更多选项&nbsp;<span style="font-size:18px;color:#DDD">&or;</span></p><p style="width:43%;float:left;height:23px;border-top:1px solid #DDD"></p>');
	}
};
var mouseovermore=function(){
	$(".item-more p").css("color","#47B9FC");
	$(".item-more p span").css("color","#47B9FC");
	$(".item-more p").css("border-color","#47B9FC");
};
var mouseleavemore=function(){
	$(".item-more p").css("color","#DDD");
	$(".item-more p span").css("color","#DDD");
	$(".item-more p").css("border-color","#DDD");
};
AI.AdvanceFilter = Event.$extend({
	id: 'FilterArea',
	hovertype: 'hover',
	containerId: 'advance_filterarea',
	TextFilter: Event.$extend({
		__init__: function(config) {
			$('#' + config.containerId).append('<input type="text"  class="input-sm form-control" placeholder="'+(config.placeholder||config.name)+'"/>');
			this.bindEvent(config);
		},
		bindEvent: function(config) {
			//默认值选中
			if (config.defaultValue) {
				$("#" + config.containerId + " input ").val(config.defaultValue);
				$("#" + config.containerId + " input").change();
			}
			var self = this;
			self.value = [];
			self.where = null;
			$("#" + config.containerId + " input").change(function() {
				self.value = [$(this).val()];
				self.where = config.where.replace(/{selfval}/g, $(this).val());
				self.fireEvent('advancefilter'+config.containerId.replace("_"+config.code, ""));
			});
		}
	}),
	LabelFilter: Event.$extend({
		__init__: function(config) {
			this.id = config.code;
			this.config = config;
			var html = '<ul class="nav navbar-nav af-value nav-label-filter"><li><a class="active all" href="javascript:;" data-value="all">所有</a></li>';
//			var sql = rebuildSQL(config.storesql);
//			var data=ai.getStoreData(sql);
			var sql = rebuildSQL(config.storesql,config.storeval);
			sql = rebuildSqlByPermission(sql,metadbListPermission(),config.name);
			var data = getItemData(sql);
			for (var i = 0, len = data.length; i < len; i++) {
				var label = _.values(data[i]);
				if (typeof(data[i]) == "string") label[0] = data[i];
				html += '<li><a href="javascript:;" data-value="' + label[0] + '">' + (label[1] || label[0]) + '</a></li>';
			}
			html += '</ul>';
			$('#' + config.containerId).append(html);
			var fullHeihgt = $('#' + config.containerId+' .nav-label-filter').height();
			if(fullHeihgt>30) {
				$('#' + config.containerId+' .af-value').css('height','30px');
				$('#' + config.containerId).append('<div class="af-more glyphicon glyphicon-chevron-down"><a href="javascript:;" class="more-items">更多</a></div>');
			}
			//if(config.limitOneLine == "y") html += '<div class="af-more glyphicon glyphicon-chevron-down"><a href="javascript:;">更多</a></div>';
			this.bindEvent(config);
		},
		bindEvent: function(config) {
			var self = this;
			self.value = [];
			self.where = null;
			//默认值选中
			if (config.defaultValue && $("#" + config.containerId + " a[data-value=" + config.defaultValue + "]").length > 0) {
				$("#" + config.containerId).find("a.active").removeClass("active");
				$("#" + config.containerId + " a[data-value=" + config.defaultValue + "]").addClass("active");
				// self.value[0] = config.defaultValue;
				// self.where = config.where.replace(/{selfval}/g, self.value.join("','"));
				// self.fireEvent('advancefilter');
			}
			$("#" + config.containerId + " a").not(".more-items").click(function() {
				var advanceId = config.containerId.replace('_'+config.code,'');
				if ($(this).hasClass("all")) {
					$("#" + config.containerId).find("a.active").removeClass("active");
					$(this).addClass("active");
					self.value = [];
					self.where = null;
				} else {
					self.value = [];
					self.where = null;
					$(this).toggleClass("active");
					//单选
					if (config.radioflag && config.radioflag.toLowerCase() == "y") {
						$("#" + config.containerId).find("a.active").not(this).removeClass("active");
					}
					if ($("#" + config.containerId).find("a.active").length == 0) $("#" + config.containerId).find("a.all").addClass("active");
					else {
						$("#" + config.containerId).find("a.all").removeClass("active");
					}
					//每次修改重新读取选中项
					var allSelectItems = $("#" + config.containerId + " a.active");
					if(allSelectItems.length ==1 && $(allSelectItems[0]).attr("data-value")=='all'){
						
					}else{
						for (var i = 0,len = allSelectItems.length; i < len; i++) {
							self.value[self.value.length] = $(allSelectItems[i]).attr("data-value");
						}
						self.where = config.where.replace(/{selfval}/g, self.value.join("','"));
					}
				}
				if(config.checkItems){
					var checkItems = config.checkItems.split(',');
					for(var i=0;i<checkItems.length;i++){
						$('#'+advanceId+'_'+checkItems[i]).parents('.media').show();
						/*if(self.where)
							$('#'+advanceId+'_'+checkItems[i]).parents('.media').show();
						else 
							$('#'+advanceId+'_'+checkItems[i]).parents('.media').hide();*/
					}
					self.fireEvent('refreshCheckItems'+config.containerId.replace("_"+config.code, ""),self.value[0],config.checkItems);
				}
				self.fireEvent('advancefilter'+config.containerId.replace("_"+config.code, ""));
			});
		}
	}),
	TreeFilter: Event.$extend({
		__init__: function(config) {
			//将jsonstore按照parentcode转换为json
			/*格式
			[{code:"", name:"", child:[{code:"", name:"", child:[ ... ] },{...}] },
			{...}]
			*/
			this.id = config.code;
			this.config = config;
			var treeGroup=[];
			var sql = rebuildSQL(config.storesql,config.storeval);
			var treeStore=ai.getJsonStore(sql);
			for(var i = 0, len = treeStore.getCount(); i < len; i++){
				var _record = treeStore.getAt(i);
				if(_record.get("PARENTCODE")) continue;
				var tempGroup = {groupCode: _record.get("CODE"),groupName: _record.get("NAME"), child:[]};
				treeGroup.push(tempGroup);
				getChildTree(tempGroup);
			}
			function getChildTree(_parentGroup){
				for(var i = 0, len = treeStore.getCount(); i < len; i++){
					var _record = treeStore.getAt(i);
					var _parentcode = _parentGroup.groupCode
					if(_record.get("PARENTCODE") != _parentcode) continue;
					var tempGroup = {groupCode: _record.get("CODE"),groupName: _record.get("NAME"), child:[]};
					_parentGroup.child.push(tempGroup);
					getChildTree(tempGroup);
				}
			}
			var tabHtml = '<ul class="nav nav-tabs nav-treezz af-value"><li><a class="active all" href="javascript:;" data-value="all">所有</a></li>';
			var contentHtml = '<div class="tab-content tab-treezz">';
			//一级
			for (var i = 0, ilen = treeGroup.length; i < ilen; i++) {
				var label = treeGroup[i];
				var firstLevelId = config.containerId+label.groupCode;
				tabHtml += '<li><a href="#tab-'+ firstLevelId +'" data-value="' + label.groupCode + '" data-toggle="tab">' + label.groupName + '</a></li>';
				if(label.child.length == 0) {continue;}
				contentHtml += '<div class="tab-pane fade" id="tab-' + firstLevelId + '" style="width:80%;height:auto;">';
				//二级
				contentHtml +='<ul class="nav second-level"><div id="'+label.groupCode+'_div"><span class="fp" style="color:#DDD">当前位置：</span><span class="sp" style="color:#47B9FC;">'+label.groupName+'</span><span style="color:#DDD"></span><span class="tp" style="color:#47B9FC;"></span></div>';
				for(var j = 0, jlen = label.child.length; j < jlen; j++){
					var secondLabel = label.child[j];
					contentHtml +='<li class="'+secondLabel.groupCode+'_parents"><a href="javascript:;" data-value="' + secondLabel.groupCode +'" levelName="second">' + secondLabel.groupName 
								+ '</a><i></i></li>';
					if(secondLabel.length == 0) continue;
					//三级及其以下
//					contentHtml +='<ul class="nav navbar-nav third-level">';
					function getThirdLabel(secondLabel){
						for(var k = 0, klen = secondLabel.child.length; k < klen; k++){
							var thirdLabel = secondLabel.child[k];
							contentHtml +='<li class="'+secondLabel.groupCode+'_children" style="display:none;"><a href="javascript:;" data-value="' + thirdLabel.groupCode +'"  levelName="third">' + thirdLabel.groupName + '</a></li>';
							if(thirdLabel.child.length > 0){
								getThirdLabel(thirdLabel);
							}
						}
					}
					getThirdLabel(secondLabel);
//					contentHtml +='</ul>';
				}
				contentHtml +='</ul></div>';
			}
			tabHtml += '</ul>';
			contentHtml += '</div>';
			//if(config.limitOneLine == "y") contentHtml += '<div class="af-more glyphicon glyphicon-chevron-down"><a href="javascript:;">更多</a></div>';
			$('#' + config.containerId).append(tabHtml + contentHtml);
			var fullHeihgt = $('#' + config.containerId+' .nav-treezz').height();
			if(fullHeihgt>30) {
				$('#' + config.containerId).append('<div class="af-more glyphicon glyphicon-chevron-down" style="margin-top:-20px;"><a href="javascript:;" class="more-items">更多</a></div>');
				$('#' + config.containerId+' .af-value').css('height','30px');
			}
			this.bindEvent(config);
		},
		bindEvent: function(config) {
			var self = this;
			self.value = [];
			self.where = null;
			//默认值选中
			if (config.defaultValue && $("#" + config.containerId + " a[data-value=" + config.defaultValue + "]").length > 0) {
				$("#" + config.containerId).find("a.active").removeClass("active");
				$("#" + config.containerId + " a[data-value=" + config.defaultValue + "]").addClass("active");
				// self.value[0] = config.defaultValue;
				// self.where = config.where.replace(/{selfval}/g, self.value.join("','"));
				// self.fireEvent('advancefilter');
			}
			//弹出浮动层挨着选中的一级菜单
			$("#" + config.containerId + " .nav-treezz a").each(function(){
				var _id = $(this).data("value");
				$("#tab-" + _id).css("top",$(this).offset().top + 27);
			});
			//移进tabcontent时不隐藏
			$(".tab-treezz>.tab-pane").hover(
				function(){
					$(this).show();
					$(".nav-treezz").addClass("nav-hover");
				},
				function(){
					$(this).hide();
					$(".nav-treezz").removeClass("nav-hover");
					if($(this).find("ul div span:eq(3)").text()!=""){
						$(this).find("ul div span:eq(1)").css({"cursor":"default","color":"#47B9FC"});
						$(this).find("ul div span:eq(2)").text("");
						$(this).find("ul div span:eq(3)").text("");
						$(this).parent().find("li[class$='_children']").css("display","none");
						$(this).parent().find("li[class$='_parents']").css("display","block");
					}
			});
			$("#" + config.containerId + " a").not(".more-items").click(function() {
				if ($(this).hasClass("all")) {
					$("#" + config.containerId).find("a.active").removeClass("active");
					$(this).addClass("active");
					self.value = [];
					self.where = null;
				} else {
					self.value = [];
					$("#" + config.containerId).find("a.all").removeClass("active");
					$(this).toggleClass("active");
					//单选
					$("#" + config.containerId).find("a.active").not(this).removeClass("active");
					//取消选择时 选中所有
					if ($("#" + config.containerId).find("a.active").length == 0) $("#" + config.containerId).find("a.all").addClass("active");
					//每次修改重新读取选中项
					var allSelectItems = $("#" + config.containerId + " a.active");
					for (var i = 0, len = allSelectItems.length; i < len; i++) {
						self.value[self.value.length] = $(allSelectItems[i]).attr("data-value");
					}
					self.where = config.where.replace(/{selfval}/g, self.value.join("','"));
				}
				self.fireEvent('advancefilter'+config.containerId.replace("_"+config.code, ""));
			});
			//点击一级菜单时移除鼠标事件，再次点击加上
			$("#" + config.containerId + " .nav-treezz a").bind( "click",function(){
				$("#tab-" + $(this).data("value")).hide();
			});
			$("#" + config.containerId + " .second-level a[levelName='second']").bind( "click",function(){//当前层级为第二层，则显示第三层，第二层全部隐藏
				var thisGroupCode=$(this).attr("data-value");
				if($(this).parent().parent().find("li[class='"+thisGroupCode+"_children']").length<1){//如果没有第三层，返回
					return;
				}
				$(this).parent().parent().find("li").css("display","none");
				$(this).parent().parent().find("li[class='"+thisGroupCode+"_children']").css("display","block");
				$(this).parent().parent().find("div span:eq(1)").css({"cursor":"pointer","color":"#DDD"});
				$(this).parent().parent().find("div span:eq(2)").text(">>");
				$(this).parent().parent().find("div span:eq(3)").text( $(this).text());
			});
			$("#" + config.containerId + " .second-level .sp").bind( "click",function(){//点击按钮返回上一层,三级元素隐藏，二级元素展示
				if($(this).parent().find("span:eq(3)").text()==""){
					return;
				}
				$(this).parent().parent().find("li[class$='_children']").css("display","none");
				$(this).parent().parent().find("li[class$='_parents']").css("display","block");
				$(this).parent().find("span:eq(1)").css({"cursor":"default","color":"#47B9FC"});
				$(this).parent().find("span:eq(2)").text("");
				$(this).parent().find("span:eq(3)").text("");
			});
			$("#" + config.containerId + " .nav-treezz a").bind( "mouseover mouseleave",function(e){
				var ypos = $(this).parent().position().top + $(this).parent().height();
				// var xpos = $(this).position().left;
				if(e.type == "mouseover"){
					$(".tab-treezz>.tab-pane").hide();
					$(".nav-treezz").addClass("nav-hover");

					var _id = $(this).attr("href").replace("#", "");
					if(_id == "all"||_id == 'javascript:;') return;

					$("#" + _id).css({
						// 'position' : 'relative',
						// 'left' : xpos + 'px',
						'top' : ypos + 'px'
					});

					$("#" + _id).show();
					$(this).tab('show');
//					$("#" + _id).css("width", $("#" + config.containerId + " .nav-treezz").width() + 4);//弹出层宽度
				}else{
					$(".nav-treezz").removeClass("nav-hover");
					var _id = $(this).attr("href");
					if(_id == "all"||_id == 'javascript:;') return;
					$(_id).hide();
					// $("#tab-" + $(this).data("value")).hide();   
				}
			});
		}
	}),
	//高级筛选下拉菜单选项框
	DropDownFilter: Event.$extend({
		__init__: function(config) {
			//将jsonstore按照parentcode转换为json
			/*格式
			[{code:"", name:"", child:[{code:"", name:"", child:[ ... ] },{...}] },
			{...}]
			*/
			this.id = config.code;
			this.config = config;
			var treeGroup=[];
			var sql = rebuildSQL(config.storesql,config.storeval);
			var treeStore=ai.getJsonStore(sql);
			for(var i = 0, len = treeStore.getCount(); i < len; i++){
				var _record = treeStore.getAt(i);
				if(_record.get("PARENTCODE")) continue;
				var tempGroup = {groupCode: _record.get("CODE"),groupName: _record.get("NAME"), child:[]};
				treeGroup.push(tempGroup);
				getChildTree(tempGroup);
			}
			function getChildTree(_parentGroup){
				for(var i = 0, len = treeStore.getCount(); i < len; i++){
					var _record = treeStore.getAt(i);
					var _parentcode = _parentGroup.groupCode
					if(_record.get("PARENTCODE") != _parentcode) continue;
					var tempGroup = {groupCode: _record.get("CODE"),groupName: _record.get("NAME"), child:[]};
					_parentGroup.child.push(tempGroup);
					getChildTree(tempGroup);
				}
			}
			var tabHtml = '<ul class="nav nav-tabs nav-dropdown af-value"><li><a class="active all" href="javascript:;" data-value="all">所有</a></li>';
			var contentHtml = '<div class="tab-content tab-dropdown">';
			//一级
			for (var i = 0, ilen = treeGroup.length; i < ilen; i++) {
				var label = treeGroup[i];
				var firstLevelId = config.containerId+label.groupCode;
				tabHtml += '<li><a href="#tab-'+ firstLevelId +'" data-value="' + label.groupCode + '" data-toggle="tab">' + label.groupName + '<span style="font-size: 18px; color: rgb(221, 221, 221);">∨</span></a></li>';
				if(label.child.length == 0) {continue;}
				contentHtml += '<div class="tab-pane fade" id="tab-' + firstLevelId + '">';
				//二级
				contentHtml +='<ul class="nav second-level">';
				for(var j = 0, jlen = label.child.length; j < jlen; j++){
					var secondLabel = label.child[j];
					contentHtml +='<li><a href="javascript:;" data-value="' + secondLabel.groupCode +'">' + secondLabel.groupName 
								+ '</a><i></i></li>';
					if(secondLabel.length == 0) continue;
				}
				contentHtml +='</ul></div>';
			}
			tabHtml += '</ul>';
			contentHtml += '</div>';
			//if(config.limitOneLine == "y") contentHtml += '<div class="af-more glyphicon glyphicon-chevron-down"><a href="javascript:;">更多</a></div>';
			$('#' + config.containerId).append(tabHtml + contentHtml);
			var fullHeihgt = $('#' + config.containerId+' .nav-dropdown').height();
			if(fullHeihgt>30) {
				$('#' + config.containerId).append('<div class="af-more glyphicon glyphicon-chevron-down" style="margin-top:-20px;"><a href="javascript:;" class="more-items">更多</a></div>');
				$('#' + config.containerId+' .af-value').css('height','30px');
			}
			this.bindEvent(config);
		},
		bindEvent: function(config) {
			var self = this;
			self.value = [];
			self.where = null;
			//默认值选中
			if (config.defaultValue && $("#" + config.containerId + " a[data-value=" + config.defaultValue + "]").length > 0) {
				$("#" + config.containerId).find("a.active").removeClass("active");
				$("#" + config.containerId + " a[data-value=" + config.defaultValue + "]").addClass("active");
				// self.value[0] = config.defaultValue;
				// self.where = config.where.replace(/{selfval}/g, self.value.join("','"));
				// self.fireEvent('advancefilter');
			}
			//弹出浮动层挨着选中的一级菜单
			$("#" + config.containerId + " .nav-dropdown a").each(function(){
				var _id = $(this).data("value");
				$("#tab-" + _id).css("top",$(this).offset().top + 27);
			});
			//移进tabcontent时不隐藏
			$(".tab-dropdown>.tab-pane").hover(
				function(){
					$(this).show();
					$(".nav-dropdown").addClass("nav-hover");
				},
				function(){
					$(this).hide();
					$(".nav-dropdown").removeClass("nav-hover");
			});
			$("#" + config.containerId + " a").not(".more-items").click(function() {
				if ($(this).hasClass("all")) {
					$("#" + config.containerId).find("a.active").removeClass("active");
					$(this).addClass("active");
					self.value = [];
					self.where = null;
				} else {
					self.value = [];
					$("#" + config.containerId).find("a.all").removeClass("active");
					$(this).toggleClass("active");
					//单选
					$("#" + config.containerId).find("a.active").not(this).removeClass("active");
					//取消选择时 选中所有
					if ($("#" + config.containerId).find("a.active").length == 0) $("#" + config.containerId).find("a.all").addClass("active");
					//每次修改重新读取选中项
					var allSelectItems = $("#" + config.containerId + " a.active");
					for (var i = 0, len = allSelectItems.length; i < len; i++) {
						self.value[self.value.length] = $(allSelectItems[i]).attr("data-value");
					}
					self.where = config.where.replace(/{selfval}/g, self.value.join("','"));
				}
				self.fireEvent('advancefilter'+config.containerId.replace("_"+config.code, ""));
			});
			//点击一级菜单时移除鼠标事件，再次点击加上
			$("#" + config.containerId + " .nav-dropdown a").bind( "click",function(){
				$("#tab-" + $(this).data("value")).hide();
			});
			$("#" + config.containerId + " .nav-dropdown a").bind( "mouseover mouseleave",function(e){
				var ypos = $(this).parent().position().top + $(this).parent().height();
				// var xpos = $(this).position().left;
				if(e.type == "mouseover"){
					$(".tab-dropdown>.tab-pane").hide();
					$(".nav-dropdown").addClass("nav-hover");

					var _id = $(this).attr("href").replace("#", "");
					if(_id == "all"||_id == 'javascript:;') return;

					$("#" + _id).css({
						// 'position' : 'relative',
						// 'left' : xpos + 'px',
						'top' : ypos + 'px'
					});

					$("#" + _id).show();
					$(this).tab('show');
					$("#" + _id).css("width", $("#" + config.containerId + " .nav-dropdown").width() + 4);//弹出层宽度
					$(this).find('span')[0].textContent = '∧';
				}else{
					$(".nav-dropdown").removeClass("nav-hover");
					var _id = $(this).attr("href");
					if(_id == "all"||_id == 'javascript:;') return;
					$(_id).hide();
					$(this).find('span')[0].textContent = '∨';
					// $("#tab-" + $(this).data("value")).hide();   
				}
			});
		}
	}),
	__init__: function(options) {
		if (!options.containerId || !$('#' + options.containerId)[0]) {
			alert('未设置容器ID或容器不存在!');
			return;
		}
		this.containerId = options.containerId ? options.containerId: this.containerId;
		options.containerId = this.containerId;
		if (options.hovertype) this.hovertype = options.hovertype;
		this.configItems = options.configItems || [];
		this.init(options);
	},
	init: function(options) {
		$("#" + this.containerId).empty();
		this.items = new Array();
//		var data = this.getItemData();
		for (var index = 0, len = this.configItems.length; index < len; index++) {
			var itemConfig = this.configItems[index];
//			if (data[itemConfig.code]) itemConfig.data = data[itemConfig.code];
			itemConfig.containerId =  this.containerId+'_'+itemConfig.code;
			var itemContainerHtml = '<article class="media" > <a class="media-left pull-left m-t-xs"><strong>' + itemConfig.name + ':</strong></a><div class="media-body" id="' +itemConfig.containerId+ '"> </div></article>';
			if(index>2){
				var itemContainerHtml = '<article class="media" style="display:none" name="hiddenArt"> <a class="media-left pull-left m-t-xs"><strong>' + itemConfig.name + ':</strong></a><div class="media-body" id="' +itemConfig.containerId+ '"> </div></article>';
			}
			//itemConfig.code =  options.containerId + " #" + itemConfig.code;
			$("#" + this.containerId).append(itemContainerHtml);
			if(itemConfig.dependences) {
				$('#'+itemConfig.containerId).parents('.media').hide();
			}
			var item = null;
			switch (itemConfig.type) {
			case 'label':
				item = new this.LabelFilter(itemConfig); break;
			case 'text':
				item = new this.TextFilter(itemConfig); break;
			case 'tree':
				item = new this.TreeFilter(itemConfig); break;
			case 'dropDown':
				item = new this.DropDownFilter(itemConfig);break;
			}
			this.items.push(item);				
		}
		if(this.configItems.length>3){
			$("#" + this.containerId).append('<div style="width:100%;height:25px;cursor:pointer;" class="item-more" onClick="showOrHidden(this)" onmouseover="mouseovermore()" onmouseleave="mouseleavemore()"><p style="width:43%;float:left;height:23px;border-top:1px solid #DDD"></p><p style="width:14%;height:23px;float:left;border-bottom:1px solid #DDD;border-left:1px solid #DDD;border-right:1px solid #DDD;text-align:center;font-size:15px;line-height:23px;color:#DDD">更多选项&nbsp;<span style="font-size:18px;color:#DDD">&or;</span></p><p style="width:43%;float:left;height:23px;border-top:1px solid #DDD"></p></div>');
		}
		this.bindEvent(options);
	},
	bindEvent:function(options) {
		var self = this;
		this.on('refreshCheckItems'+this.containerId, function(curVal,checkItems) {
			checkItems = checkItems.split(',');
			var items = $.extend(true, [], self.items);
			for (var i = 0; i < items.length; i++) {
				var item = items[i];
				for(var j=0;j<checkItems.length;j++){
					var checkItem = checkItems[j];
					if(item.id == checkItem){
						$('#'+item.config.containerId).empty();
						item.config.storeval = curVal;
						switch (item.config.type) {
						case 'label':
							self.items[i] = new self.LabelFilter(item.config); break;
						case 'text':
							self.items[i] = new self.TextFilter(item.config); break;
						case 'tree':
							self.items[i] = new self.TreeFilter(item.config); break;
						case 'dropDown':
							self.items[i] = new self.DropDownFilter(item.config); break;
						}
//						self.items.push(newItem);
					}
				}
			}
			//moreFunc();
		});
		this.on('advancefilter'+this.containerId, function(clickfun) {
			var wheres = [];
			var vals = [];
			for (var i = 0, len = self.items.length; i < len; i++) {
				var _item = self.items[i];
				if (_item.where) wheres.push(_item.where);
			}
			if (options.onChange) {
				options.onChange(wheres, vals);
			}
			console.log(wheres);
		});
		var moreFunc = function(){
			$(".af-more").click(function(){
				var $div = $(this).siblings(".af-value");
				if($(this).hasClass("glyphicon-chevron-up")) {
					$(this).removeClass("glyphicon-chevron-up");
					$(this).addClass("glyphicon-chevron-down");
					$div.css('height','30px');
				}else {
					$(this).removeClass("glyphicon-chevron-down");
					$(this).addClass("glyphicon-chevron-up");
					$div.css('height','auto');
				}
	//			$div.attr("style") && $div.attr("style").indexOf("auto") > 0 
	//				? $div.removeAttr("style") : $div.css("height","auto");
				var $a = $($(this).find("a")[0]);
				$a.html() == "收起" ? $a.html("更多") : $a.html("收起");
			});
		}
		moreFunc();
	},
	getItemData: function() {
		var configItems = this.configItems;
		var paraItems = [];
		var result = {};
		for (var index = 0, len = configItems.length; index < len; index++) {
			var item = configItems[index];
			if (!item.storesql) continue;
			if (item.storesql.toLowerCase().indexOf("select ") >= 0) {
				var para = {
					paraname: item.code,
					paratype: 'array',
					sql: item.storesql
				};
				paraItems.push(para);
			} else if (item.storesql.indexOf("|") >= 1) {
				var tmpArray = item.storesql.split("|");
				var propArray = [];
				for (var i = 0, tmpLen = tmpArray.length; i < tmpLen; i++) {
					propArray.push({
						value: tmpArray[i].split(",")[0],
						name: tmpArray[i].split(",")[1]
					});
				}
				result[item.code] = propArray;
			} else {
				result[item.code] = item.storesql.split(",");
			}
		}
		var sendObj = {
			paras: paraItems
		};
		var URL = '/' + contextUrl + '/olapquery?json=' + ai.encode(sendObj);
		var obj = ai.remoteData(URL);
		for (var i = 0, objLen = obj.root.length; i < objLen; i++) {
			result = $.extend(result, obj.root[i]);
		}
		return result;
	}
});